export default {
    name: "app",
    data() {
        // 当前密码规则
        let validator_current_password = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.current_password) {
                callback(new Error('当前密码错误!'));
            } else {
                callback();
            }
        };
        // 新密码规则
        let validator_new_password1 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value === this.current_password) {
                callback(new Error('新旧密码不能一致!'));
            } else {
                callback();
            }
        };
        // 确认新密码规则
        let validator_new_password2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.input_password.new_password1) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            current_password: '',
            input_password: {
                old_password: '',
                new_password1: '',
                new_password2: '',
            },
            info: {
                newPassword: '',
                userName: ''
            },
            input_password_Rules: {
                old_password: [
                    { required: true, message: '密码不能为空！', trigger: 'blur' },
                    // 失去焦点时，校验长度的最小值最大值，如果不符合有校验提示
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
                    { validator: validator_current_password, trigger: 'blur' },
                ],
                new_password1: [
                    { required: true, message: '密码不能为空！', trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
                    { validator: validator_new_password1, trigger: 'blur' },
                ],
                new_password2: [
                    { required: true, message: '密码不能为空！', trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
                    { validator: validator_new_password2, trigger: 'blur' },
                ],
            },
        }
    },
    created() {
        this.current_password = this.$store.state.current.password
        this.info.userName = this.$store.state.current.userName
    },
    methods: {
        change_password() {
            this.$refs.passwordRef.validate(async valid => {
                if (!valid) {
                    this.$store.commit("error", "信息不合法或为空！请重新输入");
                    this.$refs.passwordRef.resetFields();
                }
                else {
                    this.info.newPassword = this.input_password.new_password2

                    const info = this.$qs.parse(this.info)
                    const { data: res } = await this.$http.post('/gim/api/user/updatePassword', info);
                    // console.log(res)
                    if (res.code == 1) {
                        this.$refs.passwordRef.resetFields();

                        window.sessionStorage.clear();
                        this.$router.push("/login_register");
                        this.$store.commit("success", res.response + ',请重新登录。');

                    }
                    else {
                        this.$store.commit('error', res.response)
                        this.$refs.passwordRef.resetFields();
                    }
                }
            })
        },
        // 光标下移
        focusNext(nextRef) {
            this.$refs[nextRef].focus()
        }
    },
}
